package com.ybzl.gateway.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ybzl.gateway.domain.Beacon;
import com.ybzl.gateway.domain.Fence;
import com.ybzl.gateway.domain.dto.FenceDTO;
import com.ybzl.gateway.domain.dto.FenceStatusDTO;
import com.ybzl.gateway.mapper.FeneceMapper;
import com.ybzl.gateway.service.IDeviceService;
import com.ybzl.gateway.service.IFenceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DS("location_meta")
@Service
public class IFeneceServiceImpl implements IFenceService {

    @Autowired
    private FeneceMapper feneceMapper;

    @Autowired
    private IDeviceService deviceService;

    @Override
    public int saveOrUpdate(FenceDTO fenceDto) {
//        try {
//            Fence fence = new Fence();
//            BeanUtils.copyProperties(fenceDto, fence);
//            fence.setAssembly(JSONObject.toJSONString(fenceDto.getAssembly()));
//            feneceMapper.saveOrUpdate(fence);
//            // 计算当前围栏里面有哪些信标
//            String assembly = fence.getAssembly();
//            JSONArray jsonArray = JSONObject.parseArray(assembly);
//            List<PolygonGeofence.Point> points = jsonArray.toJavaList(PolygonGeofence.Point.class);
//            polygonGeofence.setPolygon(points);
//            List<Beacon> beaconList = deviceService.selectBeacon();
//            for (Beacon beacon : beaconList) {
//                boolean inside = polygonGeofence.isInside(beacon.getBeaconX(), beacon.getBeaconY());
//                if (inside) {
//                    feneceMapper.saveFenceRoom(fenceDto.getId(), beacon.getBeaconId());
//                }
//            }
//            return 1;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return 0;
    }

    @Override
    public List<Fence> selectFence() {
        return feneceMapper.selectFence();
    }

    @Override
    public int delByTs(Long ts) {
        try {

            String fenceId = feneceMapper.selectFenceByTs(ts);
            feneceMapper.delByTs(ts);
            List<Long> tsList = feneceMapper.selectFenceByFenceId(fenceId);
            for (Long aLong : tsList) {
                feneceMapper.delFenceRoomByTs(aLong);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void insertFenceStatus(Long ts, String tagId, String id, Integer way) {
        feneceMapper.insertFenceStatus(ts, tagId, id, way);
    }

    @Override
    public FenceStatusDTO selectFenceStatus(String tagId, String fenceId) {
        return feneceMapper.selectFenceStatus(tagId, fenceId);
    }

    @Override
    public String selectFenceRoomByBeaconId(String beaconId) {
        return feneceMapper.selectFenceRoomByBeaconId(beaconId);
    }

    @Override
    public String selectFenceByFenceIdStr(String fenceId) {
        return feneceMapper.selectFenceByFenceIdStr(fenceId);
    }
}
